<?php
function getMillisecond()
{

    list($t1, $t2) = explode(' ', microtime());

    return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);

}

// 查课接口设置
function getWk($type, $noun, $school, $user, $pass, $name = false)
{
    global $DB;
    global $wk;
    $a = $DB->get_row("select * from qingka_wangke_huoyuan where hid='{$type}' ");
    $type = $a["pt"];
    $cookie = $a["cookie"];
    $token = $a["token"];
    //流年查课接口
    if ($type == "liunian") {
        $data = array("uid" => $a["user"], "key" => $a["pass"], "school" => $school, "user" => $user, "pass" => $pass, "platform" => $noun, "kcid" => $kcid);
        $dx_rl = $a["url"];
        $dx_url = "$dx_rl/api.php?act=get";
        $result = get_url($dx_url, $data);
        $result = json_decode($result, true);
        return $result;
    }


    if ($type == "29") {
        $data = array("uid" => $a["user"], "key" => $a["pass"], "school" => $school, "user" => $user, "pass" => $pass, "platform" => $noun, "kcid" => $kcid);
        $ace_rl = $a["url"];
        $ace_url = "$ace_rl/api.php?act=get";
        $result = get_url($ace_url, $data);
        $result = json_decode($result, true);
        return $result;
    } //蜜雪国开
    else if ($type == "mixuegk") {
        $data = array("uid" => $type,"uid" => $a["user"], "key" => $a["pass"], "school" => $school, "user" => $user, "pass" => $pass, "platform" => $noun, "kcid" => $kcid);
        $ace_rl = $a["url"];
        $ace_url = "$ace_rl/api.php?act=get";
        $result = get_url($ace_url, $data);
        $result = json_decode($result, true);
        return $result;
    }


    else {
        print_r("没有了,文件ckjk.php,可能故障：参数缺少，比如平台名错误！！！");
        die;
    }
}

//蜜雪国开
function adjustDaysToNearestRule($days)
{
    // 定义区间边界
    $dayBoundaries = [5, 10, 15, 20, 25, 50];

    foreach ($dayBoundaries as $boundary) {
        if ($days < $boundary) {
            return $boundary === 5 ? 5 : $dayBoundaries[array_search($boundary, $dayBoundaries) - 1];
        }
    }

    // 如果 $days 大于等于最后一个边界，则返回最后一个区间的值
    return end($dayBoundaries);
}

// 蜜雪国开 获取价格
function getPriceWk2($times, $days, $addprice, $uid)
{
    include_once(ROOT . '/gkprice.php'); //须在存放授权代码目录内创建一个空白的authcode.php文件即可

    define('cid1', $cid1);
    define('cid2', $cid2);
    define('cid3', $cid3);
    define('cid4', $cid4);
    define('cid5', $cid5);
    define('cid6', $cid6);
    define('cid7', $cid7);
    define('cid8', $cid8);
    define('cid9', $cid9);
    define('cid10', $cid10);
    define('cid11', $cid11);
    define('cid12', $cid12);
    define('cid13', $cid13);
    define('cid14', $cid14);
    global $DB;
    $days = adjustDaysToNearestRule($days);
// 定义价格区间规则
    $priceRules = [
        ['days' => 5, 'times' => 500, 'cid' => cid1],
        ['days' => 5, 'times' => 1200, 'cid' => cid2],
        ['days' => 5, 'times' => 2000, 'cid' => cid3],
        ['days' => 10, 'times' => 800, 'cid' => cid4],
        ['days' => 15, 'times' => 800, 'cid' => cid5],
        ['days' => 15, 'times' => 500, 'cid' => cid6],
        ['days' => 15, 'times' => 2000, 'cid' => cid7],
        ['days' => 15, 'times' => 2500, 'cid' => cid8],
        ['days' => 20, 'times' => 600, 'cid' => cid9],
        ['days' => 20, 'times' => 2000, 'cid' => cid10],
        ['days' => 20, 'times' => 3000, 'cid' => cid11],
        ['days' => 25, 'times' => 5000, 'cid' => cid12],
        ['days' => 50, 'times' => 6000, 'cid' => cid13],
        ['days' => 20, 'times' => 4000, 'cid' => cid14],
    ];

    // 遍历规则，匹配对应的区间
    foreach ($priceRules as $rule) {
        if ($days == $rule['days'] && $times <= $rule['times']) {
            $cid = $rule['cid'];
            break;
        }
    }
    if ($cid == null) {
        $resultData = '{"code":1,"msg":"查询失败，超出范围","price":0,"cid":0}';
    } else {
        $rs = $DB->get_row("select * from qingka_wangke_class where cid='$cid' limit 1 ");

        if ($rs['yunsuan'] == "*") {
            $danjia = round($rs['price'] * $addprice, 2);
        } elseif ($rs['yunsuan'] == "+") {
            $danjia = round($rs['price'] + $addprice, 2);

        } else {
            $danjia = round($rs['price'] * $addprice, 2);
        }

        //密价
        $mijia = $DB->get_row("select * from qingka_wangke_mijia where uid='{$uid}' and cid='$cid' ");

        if ($mijia) {
            if ($mijia['mode'] == 0) {
                $danjia = round($danjia - $mijia['price'], 2);
                if ($danjia <= 0) {
                    $danjia = 0;
                }
            } elseif ($mijia['mode'] == 1) {
                $danjia = round(($rs['price'] - $mijia['price']) * $addprice, 2);
                if ($danjia <= 0) {
                    $danjia = 0;
                }
            } elseif ($mijia['mode'] == 2) {
                $danjia = $mijia['price'];
                if ($danjia <= 0) {
                    $danjia = 0;
                }
            }
        }

        if ($danjia == 0 || $addprice < 0.1) {
            $danjia = 10000;
        }
        $resultData = '{"code":1,"msg":"查询成功","price":' . $danjia . ',"cid":' . $cid . '}';
    }
    return $resultData;
}


?>
